<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/15 0015
 * Time: 15:48
 */


/**
 * Tcp异步客户端
 */
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);

//注册连接成功回调
$client->on("connect", function($cli) {
    $cli->send("hello world\n");
});

//注册数据接收回调
$client->on("receive", function($cli, $data){
    echo "Received: ".$data."\n";
});

//注册连接失败回调
$client->on("error", function($cli){
    echo "Connect failed\n";
});

//注册连接关闭回调
$client->on("close", function($cli){
    echo "Connection close\n";
});

//发起连接
$client->connect('127.0.0.1', 9501, 0.5);


/*
执行程序
php AsyServer.php
Dispath AsyncTask: id=0
New AsyncTask[id=0]
AsyncTask[0] Finish: hello world
 -> OK

php AsyClient.php
Received: Server: hello world

*/